# -*- python -*-
# ex: set syntax=python:

# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# See master.experimental/slaves.cfg for documentation.

import chromiumos_tryserver_util

from common import slave_alloc


def _GenerateBuilder(format, *ids):
  """Return a list of builder names given a format and id list.

  Arguments:
    format: Format of the name, that takes one integer id parameter.
    ids: A list of id elements.  Individual elements can be a standalone
         integer or a tuple.  Tuples are treated as inclusive ranges and
         expanded.  A tuple of (1,3) would be expanded as [1, 2, 3].
  """
  builders = set()
  for id_element in ids:
     if isinstance(id_element, tuple):
       builders.update([format % id for id in
                        xrange(id_element[0], id_element[1] + 1)])
     else:
       builders.add(format % id_element)
  return builders


sa = slave_alloc.SlaveAllocator()
sa.AddPool('baremetal', *_GenerateBuilder('build%i-m2',
    34, 42, 43, 46, 296, 312, 315, 318, 320, 329, 331, 332, 335,
    (222,233), (238, 242), 244, 247, (264, 277), (207, 220),
    9, 16, 17, 20, 25, 35, 41, 44, 49, (59, 67), 69, 70,
    ))
sa.AddPool('gce', *_GenerateBuilder('cros-standard%i-c2',
    (1, 120)))

# Define slave classes.
precq = sa.Alloc('precq', subtype='baremetal', pools=('baremetal',), count=52)
precq_novmtest = sa.Alloc('precq', subtype='gce', pools=('gce',), count=None)
general = sa.Alloc('general', pools=('baremetal',), count=None)

for builders, pools in (
    (chromiumos_tryserver_util.precq_builders, (precq,)),
    (chromiumos_tryserver_util.precq_novmtest_builders, (precq_novmtest,)),
    (chromiumos_tryserver_util.all_builders, (general,)),
    ):
  for builder in builders:
    for pool in pools:
      sa.Join(builder, pool)

def _GenerateSlave(host, entry):
  return {
        'master': 'ChromiumOSTryServer',
        'builder': list(entry.keys),
        'hostname': host,
        'os': 'linux',
        'version': 'trusty',
    }
slaves = [_GenerateSlave(slave, builders)
          for slave, builders in sa.GetSlaveMap().entries.iteritems()]
